<?php

class Application_Model_DbTable_ItemPriority extends Zend_Db_Table_Abstract
{

    protected $_name = 'txy_item_priority';

    public function addNewPriority($data){
    	try{
    		$this->insert($data);
    	}catch (Exception $e){
    		
    	}
    }
    
    public function getRelItems($numiid){
    	$res=$this->fetchAll("main_numiid='".$numiid."'");
    	if(count($res)>0){
        	foreach ($res as $data){
        		$sqlArray[]="'".$data['rel_numiid']."'";
        	}
        	$sqlStr=implode(",", $sqlArray);
    	}else{
    	    $sqlStr="";
    	}
    	return $sqlStr;
    }
    
    public function updateRelItems($numiid,$data){
        $res=$this->fetchRow("main_numiid ='".$numiid."'","rel_priority asc");
        if(isset($res)){
            $res=$res->toArray();
            if($data['rel_priority']>$res['rel_priority']){
                try{
                    $this->update($data, "id ='".$res['id']."'");
                }catch(Exception $e){
                    $code=$e->getCode();
                    if(23000==$code){
                        $res1=$this->fetchRow("main_numiid ='".$numiid."' and rel_numiid ='".$data['rel_numiid']."'");
                        $res1=$res1->toArray();
                        if($data['rel_priority']>$res1['rel_priority']){
                             try{
                                $this->update($data, "id ='".$res1['id']."'");
                             }catch(Exception $e){
                                throw new Exception("更新数据表时异常".$numiid."|".$e->getMessage());
                             }
                        }
                    }
                    
                }
            }
        }
    }
}

